/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.bld.model.anuncio;

import br.com.bld.util.facade.AbstractDao;
import br.com.bld.util.facade.EntityFactory;
import com.google.common.io.Files;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.sql.Types;
import java.util.Date;
import java.util.HashMap;
import org.postgresql.largeobject.LargeObject;
import org.postgresql.largeobject.LargeObjectManager;

/**
 *
 * @author Pedro
 */
class AnuncioDao extends AbstractDao {

    public AnuncioDao(Connection conn, EntityFactory entityFactory) {
        super(conn, entityFactory);

    }

    private AnuncioPK getPk(ResultSet rs) throws SQLException {

        int i = 0;

        AnuncioPK resposta = new AnuncioPK(rs.getLong(++i), rs.getLong(++i));

        return resposta;
    }

    private AnuncioEntity getEntity(ResultSet rs) throws SQLException, FileNotFoundException, IOException {
        AnuncioEntity resposta = new AnuncioEntity();

        int i = 2;

        resposta.setDataInicio(new Date(rs.getTimestamp(++i).getTime()));
        resposta.setDataFim(new Date(rs.getTimestamp(++i).getTime()));
        rs.getString(++i);
        if (!rs.wasNull()) {
            resposta.setImageName(rs.getString(i));
        }
        rs.getByte(++i);
        if (!rs.wasNull()) {
            byte[] array = rs.getBytes(i);
            File file = new File(resposta.getImageName());

            Files.write(array, file);

            resposta.setImage(file);
        }
        resposta.setPlanoId(rs.getLong(++i));

        return resposta;
    }

    public int insert(AnuncioEntity entity, AnuncioPK pk) throws SQLException, FileNotFoundException {

        String sql = createInsert();

        PreparedStatement ps = null;

        try {
            ps = getPreparedStatement(sql);

            int i = 0;

            ps.setLong(++i, pk.getId());
            ps.setLong(++i, pk.getClienteId());

            ps.setTimestamp(++i, new Timestamp(entity.getDataInicio().getTime()));
            ps.setTimestamp(++i, new Timestamp(entity.getDataFim().getTime()));
            if (entity.getImageName() != null && !entity.getImageName().equals("")) {
                ps.setString(++i, entity.getImageName());
            } else {
                ps.setNull(++i, Types.VARCHAR);
            }
            if (entity.getImage() != null) {
                FileInputStream fis = new FileInputStream(entity.getImage());
                ps.setBinaryStream(++i, fis, entity.getImage().length());
            } else {
                ps.setNull(++i, Types.BINARY);
            }
            ps.setLong(++i, entity.getPlanoId());

            return ps.executeUpdate();
        } finally {
            close(ps);
        }
    }

    public int update(AnuncioEntity entity, AnuncioPK pk) throws SQLException, FileNotFoundException {

        String sql = createUpdate();

        PreparedStatement ps = null;

        try {

            ps = getPreparedStatement(sql);

            int i = 0;

            ps.setTimestamp(++i, new Timestamp(entity.getDataInicio().getTime()));
            ps.setTimestamp(++i, new Timestamp(entity.getDataFim().getTime()));
            if (entity.getImageName() != null && !entity.getImageName().equals("")) {
                ps.setString(++i, entity.getImageName());
            } else {
                ps.setNull(++i, Types.VARCHAR);
            }
            if (entity.getImage() != null) {
                FileInputStream fis = new FileInputStream(entity.getImage());
                ps.setBinaryStream(++i, fis, entity.getImage().length());
            } else {
                ps.setNull(++i, Types.BINARY);
            }
            ps.setLong(++i, entity.getPlanoId());

            ps.setLong(++i, pk.getId());
            ps.setLong(++i, pk.getClienteId());

            return executeUpdate(ps);

        } finally {
            close(ps);
        }
    }

    public int delete(AnuncioPK pk) throws SQLException {
        String sql = createDelete();

        PreparedStatement ps = null;

        try {

            ps = getPreparedStatement(sql);

            int i = 0;

            ps.setLong(++i, pk.getId());

            return executeUpdate(ps);

        } finally {
            close(ps);
        }
    }

    public HashMap<AnuncioPK, AnuncioEntity> selectAll() throws SQLException, IOException {

        HashMap<AnuncioPK, AnuncioEntity> resposta = new HashMap();
        String sql = createSelect();

        PreparedStatement ps = null;
        ResultSet rs = null;

        try {

            ps = getPreparedStatement(sql);
            rs = getResultSet(ps);

            while (rs.next()) {
                resposta.put(getPk(rs), getEntity(rs));
            }

            return resposta;
        } finally {
            close(ps, rs);
        }
    }

    public AnuncioEntity selectByPk(AnuncioPK pk) throws SQLException, IOException {

        AnuncioEntity resposta = null;

        String sql = createSelectByPk();

        PreparedStatement ps = null;
        ResultSet rs = null;

        try {

            ps = getPreparedStatement(sql);

            int i = 0;

            ps.setLong(++i, pk.getId());

            rs = getResultSet(ps);

            if (rs.next()) {
                resposta = getEntity(rs);
            }

            return resposta;
        } finally {
            close(ps, rs);
        }
    }

    public HashMap<AnuncioPK, AnuncioEntity> selectByPage(Integer numrPagina, Integer qtdePorPagina) throws SQLException, IOException {

        HashMap<AnuncioPK, AnuncioEntity> resposta = new HashMap();
        String sql = createSelectByPage(numrPagina, qtdePorPagina);

        PreparedStatement ps = null;
        ResultSet rs = null;

        try {

            ps = getPreparedStatement(sql);
            rs = getResultSet(ps);

            while (rs.next()) {
                resposta.put(getPk(rs), getEntity(rs));
            }

            return resposta;
        } finally {
            close(ps, rs);
        }
    }
}
